package model

import (
	"common/globals"
	"gorm.io/gorm"
)

type Coupon struct {
	gorm.Model
	CouponName        string  `gorm:"column:coupon_name;type:varchar(50);not null" json:"coupon_name"`
	CouponPrice       float64 `gorm:"column:coupon_price;type:decimal(10,2);not null" json:"coupon_price"`
	HowMoneyAvailable float64 `gorm:"column:how_money_available;type:decimal(10,2);not null" json:"how_money_available"`
	CanBeClaimedLever int     `gorm:"column:can_be_claimed_lever;type:int;not null" json:"can_be_claimed_lever"`
	Socket            int     `gorm:"column:socket;type:int;not null;default:10" json:"socket"`
}

func (c *Coupon) TableName() string {
	return "coupon"
}
func (c *Coupon) GetCouponInfoByCanBeClaimedLever(CanBeClaimedLever int) ([]Coupon, error) {
	var coupons []Coupon
	err := globals.Db.Model(&Coupon{}).Where("can_be_claimed_lever <= ? and socket > ?", CanBeClaimedLever, 0).Find(&coupons).Error
	return coupons, err
}
func (c *Coupon) GetCouponInfoById(id int) error {
	return globals.Db.Where("id=?", id).Limit(1).Find(&c).Error
}
func (c *Coupon) UpdateSocketById(id int, socket int) error {
	return globals.Db.Model(&Coupon{}).Where("id=?", id).Update("socket", socket).Error
}
